package com.springboot.admin.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 未授权异常
     * @param req
     * @param res
     * @param e
     * @return
     * @throws Exception
     */
    @ExceptionHandler(AccessDeniedException.class)
    public ResponseMap handleAuthorizationException(HttpServletRequest req, HttpServletResponse res, Exception e) throws Exception{
        // 所有的异常都是进入该方法，并返回异常消息
        res.setStatus(HttpServletResponse.SC_FORBIDDEN);
        return ResponseMap.error("用户信息失效，请重新登录");
    }

    @ExceptionHandler(Exception.class)
    public ResponseMap globalErrorHandler(HttpServletRequest req, HttpServletResponse res, Exception e) throws Exception{
        // 所有的异常都是进入该方法，并返回异常消息
        return ResponseMap.error(e.getMessage());
    }
}
